<template>
  <a-modal
    :title="title"
    :width="400"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
  <a-row :gutter="8">
    <tree-select
      v-model="localTreeValue"
      show-search
      style="width: 100%"
      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
      placeholder="Please select"
      allow-clear
      multiple
      tree-default-expand-all
      :tree-data="treeData"
      tree-node-filter-prop="label"
    >
      <!-- <template #title="{ value: val, label }">
        <b v-if="val === 'parent 1-1'" style="color: #08c">sss</b>
        <template v-else>{{ label }}</template>
      </template> -->
    </tree-select>
    </a-row>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import STree from '@/components/Tree/Tree'
import { TreeSelect } from 'ant-design-vue';
import { getAllRole,updateUserRole } from '@/api/system'
 
const value = ['1'];
const SHOW_PARENT = TreeSelect.SHOW_PARENT;
export default {
  name: 'RoleModal',
  components: {
    STree,
    TreeSelect
  },
  props: {
    title:{
      type:String,
      required: true

    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    treeValue:{
      type:String,
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      mdl: {},

      form: this.$form.createForm(this),
      
      treeData: [],
      localTreeValue:this.treeValue,
      SHOW_PARENT:this.SHOW_PARENT
    }
  },
  watch: {
    // 监听 treeValue 的变化  
    treeValue(newValue) {
      console.log('treeValue changed:', newValue)
      this.localTreeValue = newValue
      // 在这里执行你希望在 treeValue 变化时执行的操作  
    }
  },
  created () {
    this.getAllRole()
  },
  methods: {
    getAllRole(){
      getAllRole().then(res=>{
        this.treeData=res.data
        this.treeData=[]
        res.data.forEach((element)=>{
          var obj={
          label: element.name,
          value: element.id,
          children:[]
          }
          this.treeData.push(obj)
        })
      })
      console.log(this.treeData)
    },
    handleClick (e) {
      console.log('handleClick', e)
      this.queryParam = {
        key: e.key
      }
      this.$refs.table.refresh(true)
    },
    
    handleAdd (item) {
      console.log('add button, item', item)
      this.$message.info(`提示：你点了 ${item.key} - ${item.title} `)
      this.$refs.modal.add(item.key)
    },
    handleTitleClick (item) {
      console.log('handleTitleClick', item)
    },
    add () {
      this.edit({ id: 0 })
    },
    edit (record) {
      this.mdl = Object.assign({}, record)
      this.visible = true

      // 有权限表，处理勾选 
      if (this.mdl.permissions && this.permissions) {
        // 先处理要勾选的权限结构
        const permissionsAction = {}
        this.mdl.permissions.forEach(permission => {
          permissionsAction[permission.permissionId] = permission.actionEntitySet.map(entity => entity.action)
        })
        // 把权限表遍历一遍，设定要勾选的权限 action
        this.permissions.forEach(permission => {
          permission.selected = permissionsAction[permission.id] || []
        })
      }

      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'name', 'status', 'describe'))
      })
      console.log('this.mdl', this.mdl)
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    handleOk () {
      const _this = this
      console.log(_this.model)
      console.log(_this.localTreeValue)
      var parameter={
        id:_this.model.id,
        roleIds:_this.localTreeValue
      }
      console.log(parameter)
      updateUserRole(parameter).then(res=>{
          console.log(res)
          this.visible=false
      })
      // 触发表单验证
      // this.form.validateFields((err, values) => {
      //   // 验证表单没错误
      //   if (!err) {
      //     console.log('form values', values)

      //     _this.confirmLoading = true
      //     // 模拟后端请求 2000 毫秒延迟
      //     new Promise((resolve) => {
      //       setTimeout(() => resolve(), 2000)
      //     }).then(() => {
      //       // Do something
      //       _this.$message.success('保存成功')
      //       _this.$emit('ok')
      //     }).catch(() => {
      //       // Do something
      //     }).finally(() => {
      //       _this.confirmLoading = false
      //       _this.close()
      //     })
      //   }
      // })
    },
    handleCancel () {
      this.close()
    },

  }
}
</script>

<style scoped>

</style>
